package com.quantdo.qtm.linux.service.impl;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Properties;

import org.springframework.stereotype.Service;

import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;

@Service
public class LinuxShellOperate {
    
    private static final int TIME_OUT = 1 * 60 * 1000; // 设置超时为5分钟

    public static void main(String[] args) {
        String path = "";
        LinuxShellOperate shellOperate = new LinuxShellOperate();
        Session session = shellOperate.getSession("192.168.100.172", "quantdo", "quantdo");
        shellOperate.connection(session, shellOperate.getShell(path));
        shellOperate.close(session);
    }

    public Session getSession(String ip, String user, String password) {
        JSch jsch = new JSch();
        try {
            Session session = jsch.getSession(user, ip);
            session.setPassword(password);
            Properties config = new Properties();
            config.setProperty("StrictHostKeyChecking", "no");
            config.put("UseDNS", "no");
            session.setConfig(config);
            session.setTimeout(TIME_OUT);
            return session;
        } catch (JSchException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String getShell(String path) {
        String res = "";
        try (InputStream inputStream = LinuxShellOperate.class.getResourceAsStream("monitor.sh"); 
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));) {
            String line = null;
            while (null != (line = reader.readLine())) {
                res = res.concat(line).concat("\n");
            }
            res = res.replace("%path", path);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return res;
    }

    public String[] connection(Session session, String command) {
        try (ByteArrayOutputStream out = new ByteArrayOutputStream(); 
                ByteArrayOutputStream error = new ByteArrayOutputStream();) {
            session.connect();
            ChannelExec channelExec = (ChannelExec) session.openChannel("exec");
            channelExec.setCommand(command);
            channelExec.setOutputStream(out);
            channelExec.setErrStream(error);
            channelExec.connect();
            // 确保能够执行完成及响应所有数据
            Thread.sleep(1000);
            String[] msg = new String[2];
            msg[0] = new String(out.toByteArray(), Charset.defaultCharset());
            msg[1] = new String(error.toByteArray(), Charset.defaultCharset());
            System.out.println(Arrays.toString(msg));
            return msg;
        } catch (JSchException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public void close(Session session) {
        if (null != session) {
            session.disconnect();
        }
    }
}
